// 文件位置: src/main/java/your/package/controller/ParkingRecordController.java
package your.package.controller;

import your.package.model.ParkingRecord;
import your.package.repository.ParkingRecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/parking-records")
public class ParkingRecordController {

    @Autowired
    private ParkingRecordRepository parkingRecordRepository;

    @GetMapping("/")
    public List<ParkingRecord> getAllParkingRecords() {
        return parkingRecordRepository.findAll();
    }

    @PostMapping("/")
    public ParkingRecord createParkingRecord(@RequestBody ParkingRecord parkingRecord) {
        return parkingRecordRepository.save(parkingRecord);
    }

    @PutMapping("/{id}")
    public ParkingRecord updateParkingRecord(@PathVariable Long id, @RequestBody ParkingRecord recordDetails) {
        ParkingRecord parkingRecord = parkingRecordRepository.findById(id)
            .orElseThrow(() -> new RuntimeException("Parking Record not found on :: " + id));
        parkingRecord.setVehicleId(recordDetails.getVehicleId());
        parkingRecord.setSpotId(recordDetails.getSpotId());
        parkingRecord.setStartTime(recordDetails.getStartTime());
        parkingRecord.setEndTime(recordDetails.getEndTime());
        return parkingRecordRepository.save(parkingRecord);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteParkingRecord(@PathVariable Long id) {
        ParkingRecord parkingRecord = parkingRecordRepository.findById(id)
            .orElseThrow(() -> new RuntimeException("Parking Record not found on :: " + id));
        parkingRecordRepository.delete(parkingRecord);
        return ResponseEntity.ok().build();
    }
}
